$.extend($.fn.datagrid.defaults.editors, {
    select: {
        init: function(container, options){
            var sel = '<select id="selectState">'+
                        '<option value ="0">就绪</option>'+
                         '<option value ="1">正在运行</option>'+
                         '<option value="2">成功</option>'+
                         '<option value="3">Worker内部错误</option>'+
                         '<option value="4">Worker外部错误</option>'+
                       '</select>';
            var input = $(sel).appendTo(container);
            return input;
        },
        destroy: function(target){
            alert($(target).val());
            $(target).remove();
        },
        getValue: function(target){
            return $(target).val();
        },
        setValue: function(target, value){
            $(target).val(value);
        },
        resize: function(target, width){
            $(target)._outerWidth(width);
        }
    }
});

$.extend($.fn.datagrid.methods, {
			editCell: function(jq,param){
				return jq.each(function(){
					var opts = $(this).datagrid('options');
					var fields = $(this).datagrid('getColumnFields',true).concat($(this).datagrid('getColumnFields'));
					for(var i=0; i<fields.length; i++){
						var col = $(this).datagrid('getColumnOption', fields[i]);
						col.editor1 = col.editor;
						if (fields[i] != param.field){
							col.editor = null;
						}
					}
					$(this).datagrid('beginEdit', param.index);
                    var ed = $(this).datagrid('getEditor', param);
                    if (ed){
                        if ($(ed.target).hasClass('textbox-f')){
                            $(ed.target).textbox('textbox').focus();
                        } else {
                            $(ed.target).focus();
                        }
                    }
					for(var i=0; i<fields.length; i++){
						var col = $(this).datagrid('getColumnOption', fields[i]);
						col.editor = col.editor1;
					}
				});
			},
            enableCellEditing: function(jq){
                return jq.each(function(){
                    var dg = $(this);
                    var opts = dg.datagrid('options');
                    opts.oldOnClickCell = opts.onClickCell;
                    opts.onClickCell = function(index, field){
                        if (opts.editIndex != undefined){
                            if (dg.datagrid('validateRow', opts.editIndex)){
                                dg.datagrid('endEdit', opts.editIndex);
                                opts.editIndex = undefined;
                            } else {
                                return;
                            }
                        }
                        dg.datagrid('selectRow', index).datagrid('editCell', {
                            index: index,
                            field: field
                        });
                        opts.editIndex = index;
                        opts.oldOnClickCell.call(this, index, field);
                    }
                });
            }
		});

		$(function(){
			//$('#taskList').datagrid().datagrid('enableCellEditing');
//
//			$('#selectState').on('change',changeState)

//			$('#taskList').datagrid({'onDblClickCell':function(index,field,value){
//                        alert(index + ',' + field + ',' + 'value:'+ value);
//                        $(this).datagrid('beginEdit', index);
//                        var ed = $(this).datagrid('getEditor', {index:index,field:field});
//                        $(ed.target).focus();
//			}})

            //$('#taskList').datagrid({'onAfterEdit':function(index,row,changes){
            //}});
            var toolbar = [{
                iconCls: 'icon-add',
                text: '添加',
                handler: function () {
                    $('#win').window({title: '添加'}).window('open').window('center');
                }
            },'-',{
                iconCls: 'icon-remove',
                text:'删除',
                handler: function () {
                    
                }
            }];

            var columns = [[
                {field:'id',width:80,align: 'center',title:'任务ID'},
                {field:'name',width:100,align: 'center',title:'任务名'},
                {field:'subName',width:80,align:'center',title:'子任务名'},
                {field:'type',width:80,align: 'center',title:'任务类型'},
                {field:'cronTab',width:80,align: 'center',title:'CronTab'},
                {field:'state',width:100,align: 'center',title:'任务状态码'},
                {field:'stateDesc',width:100,align: 'center',title:'任务状态描述'},
                {field:'startTime',width:100,align: 'center',title:'任务开始时间'},
                {field:'nextStartTime',width:80,align: 'center',title:'下次执行时间'},
                {field:'createDT',width:100,align: 'center',title:'任务创建时间'},
                {field:'hostName',width:100,align: 'center',title:'执行任务主机'},
                {field:'createUser',width:100,align: 'center',title:'任务创建人'}]];

            $('#taskList').datagrid({
                url: '/task/listAll',
                rownumbers: true,
                title: '任务列表',
                singleSelect:true,
                collapsible:false,
                columns: columns,
                toolbar: toolbar,
                minWidth: 300,
                minHeight: 300,
                method: 'get',
                loadMsg: '数据加载中...',
                onDblClickRow: function(index,row){
                    $('#win').window({title: '修改'}).window('open').window('center');
                    $('#taskName').val('name11111');
                    alert($('#taskName').val());
                }
            });

            var winContent = '<div style="margin: 0 auto;width: 90%; height: auto;">'
                + '<form id="taskForm">'
                + ' <div style="height:40px;padding:5px;">'
                + '     <div style="margin-right:15px;float:left;width:100px;align:center;text-align: center;line-height: 30px;height:30px">任务名:</div>'
                + '     <div style="float:left;width: 350px;"><input class="easyui-textbox" style="width:100%;height:30px;" id="taskName" name="name" type="text"/></div>'
                + '</div>'
                + ' <div style="height:40px;padding:5px;">'
                + '     <div style="margin-right:15px;float:left;width:100px;align:center;text-align: center;line-height: 30px;height:30px">子任务名:</div>'
                + '     <div style="float:left;width: 350px;"><input class="easyui-textbox" style="width:100%;height:30px;" id="taskSubName" type="text"/></div>'
                + '</div>'
                + '<div style="height:40px;padding:5px;">'
                + '  <div style="margin-right:15px;float:left;width:100px;align:center;text-align: center;line-height: 30px;height:30px">任务类型:</div>'
                + '  <select id="taskType">'
                + '     <option value="0">单次任务</option>'
                + '     <option value="1">周期任务</option>'
                + '   </select>'
                + '</div>'
                + '<div style="height:40px;padding:5px;">'
                + ' <div style="margin-right:15px;float:left;width:100px;align:center;text-align: center;line-height: 30px;height:30px">cron表达式:</div>'
                + ' <div style="float:left;width:350px;"><input class="easyui-textbox" style="width:100%;height:30px;" id="cronInput" type="text"/></div>'
                + '</div>'
                + '<div style="height:40px;padding:5px;">'
                + ' <div style="margin-right:15px;float:left;width:100px;align:center;text-align: center;line-height: 30px;height:30px">任务状态:</div>'
                + ' <select>'
                + '     <option value="0">就绪</option>'
                + '     <option value="1">运行中</option>'
                + '     <option value="2">成功</option>'
                + '     <option value="3">worker内部出错</option>'
                + '     <option value="4">worker外部出错</option>'
                + ' </select>'
                + '</div>'
                + '<div style="height:40px;padding:5px;">'
                + ' <div style="margin-right:15px;float:left;width:100px;align:center;text-align: center;line-height: 12px;font-size:12px;">任务创建人:</div>'
                + ' <div style="float:left;width:350px;"><input class="easyui-textbox" style="width:100%;height:30px;" id="createUser" /></div>'
                + '</div>'
                + '<div style="align:center;padding: 0 200px;">'
                + '<a href="#" class="easyui-linkbutton" data-options="iconCls:\'icon-ok\'" style="padding:5px 0px;width:50%;">'
                + ' <span style="font-size:14px;">确定</span>'
                + '</a>'
                + '</div>'
                + '</form>'
                + '</div>';

            $('#win').window({
                width: 600,
                height: 400,
                modal: true,
                closed: true,
                content: winContent,
                collapsible: false
            });
		})
